package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcityMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcity;
import com.unlcn.ils.erpInterfaces.dao.model.Smcitymap;
import com.unlcn.ils.erpInterfaces.dao.model.Smwarehouse;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmwarehouseMapper;
import com.unlcn.ils.erpInterfaces.service.buss.ISmwarehouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * SMWAREHOUSE 装车点  服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-08
 */
@Service
public class SmwarehouseServiceImpl extends ServiceImpl<SmwarehouseMapper, Smwarehouse> implements ISmwarehouseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmwarehouseServiceImpl.class);

    @Autowired
    private SmwarehouseMapper smwarehouseMapper;

    @Autowired
    private SmcityMapper smcityMapper;

    @Override
    public Long getSmwarehouseId(Long customerId, Long contractId, Smcity startCity) {

        //检查装车点是否存在ERP中
        Wrapper<Smwarehouse> ew = new EntityWrapper<>();
        ew.eq("ICUSTOMERID", customerId);
        ew.eq("ICONTRACTID", contractId);
        ew.eq("VCCITYNAME", startCity.getVccityname());
        ew.eq("BACTIVE", 1);
        List<Smwarehouse> smwarehouseList = smwarehouseMapper.selectList(ew);
        if (CollectionUtils.isEmpty(smwarehouseList)) {

            String province = "";
            //获取提车点的主键值
            Long ilineid = smwarehouseMapper.getNextIlineid();
            //将该提车点存入到ERP中
            Smwarehouse smwarehouse = new Smwarehouse();
            smwarehouse.setIlineid(ilineid);
            smwarehouse.setIcustomerid(customerId);
            smwarehouse.setIcontractid(contractId);
            smwarehouse.setVccityname(startCity.getVccityname());
            smwarehouse.setVcaddress(startCity.getVccityname());
            smwarehouse.setVcwarehousename(startCity.getVccityname());
            Wrapper smcityWrapper = new EntityWrapper();
            smcityWrapper.eq("VCCITYNAME", startCity.getVccityname());
            List<Smcity> smcityList = smcityMapper.selectList(smcityWrapper);
            if(CollectionUtils.isEmpty(smcityList)){
                LOGGER.error("提车点未找到对应的城市："+startCity.getVccityname());
            }else{
                province = smcityList.get(0).getVcprovince();
            }
            smwarehouse.setVcprovince(province);
            smwarehouse.setVcuserno("system");
            smwarehouse.setVcwarehouseno("H"+String.valueOf(ilineid));
            smwarehouseMapper.insert(smwarehouse);
            return ilineid;
        } else {
            return smwarehouseList.get(0).getIlineid();
        }
    }
}
